package lambda;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

public class Demo2
{
    int num = 10; //jdk 1.7以前 必须final修饰

    @Test
    public void test01(){
        //匿名内部类
        new Runnable() {
            @Override
            public void run() {
                //在局部类中引用同级局部变量
                //只读
                System.out.println("Hello World" + num);
            }
        };
    }

    @Test
    public void test02(){
        //语法糖
        Runnable runnable = () -> {
            System.out.println("Hello Lambda");
        };
        Thread thread=new Thread(runnable);
        thread.start();
    }
    @Test
    public void test03(){
        Consumer<String> consumer = (a) -> System.out.println(a);
        consumer.accept("我觉得还行！");
    }
    @Test
    public void test04(){
        Comparator<Integer> comparator = (a, b) -> {
            System.out.println("比较接口");
            return Integer.compare(a, b);
        };
        List<Integer> list= Arrays.asList(5,4,6,9);
        Collections.sort(list,comparator);
        list.forEach(a->{
            System.out.print(a);
        });
    }
}
